package com.volunteer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.volunteer.entity.Registration;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

/**
 * 报名Mapper接口
 */
@Mapper
public interface RegistrationMapper extends BaseMapper<Registration> {

    /**
     * 根据活动ID和用户ID查询报名记录
     */
    @Select("SELECT * FROM t_registration WHERE activity_id = #{activityId} AND user_id = #{userId} LIMIT 1")
    Registration selectByActivityAndUser(Long activityId, Long userId);

    /**
     * 获取用户报名记录列表(带详情)
     *
     * @param page   分页参数
     * @param userId 用户ID
     * @param status 状态：0-待审核，1-已通过，2-已拒绝，3-已取消
     * @param title  活动标题，模糊查询
     * @return 报名列表
     */
    @Select({
            "<script>",
            "SELECT r.*, a.title as activityName, a.location as activity_location, a.start_time as startTime, a.end_time as activity_end_time ",
            "FROM t_registration r ",
            "JOIN t_activity a ON r.activity_id = a.id ",
            "WHERE r.user_id = #{userId} ",
            "<if test='status != null'>",
            "  AND r.status = #{status} ",
            "</if>",
            "<if test='title != null and title != \"\"'>",
            "  AND a.title LIKE CONCAT('%', #{title}, '%') ",
            "</if>",
            "ORDER BY r.id DESC",
            "</script>"
    })
    Page<Registration> getUserRegistrationListWithDetail(Page<Registration> page,
                                                         @Param("userId") Long userId,
                                                         @Param("status") Integer status,
                                                         @Param("title") String title);
}
